package com.calque;

import java.io.InputStream;
import java.util.ArrayList;

import java.util.List;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.widget.TextView;
import android.widget.Toast;

import com.activity.MyApplicationMainActivity;
import com.commun.Param;
import com.commun.Utils;
import com.commun.Utils.CalqueEnum;
import com.geo.GeoTools;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.ItemizedOverlay;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.object.Mosque;
import com.object.OverlayItemCustom;
import com.route.*;

public class CalqueSites extends ItemizedOverlay<OverlayItemCustom> {

	/* Declration */
	private List<OverlayItemCustom> items = new ArrayList<OverlayItemCustom>();

	public static boolean isDrawingRouteActive = false;
	private Drawable marker = null;
	private Context context = null;
	OverlayItemCustom overlayItem;
	MapView mapView;
	public static Toast myToast;
	private Handler mHandler = new Handler();
	private Runnable mUpdateTimeTask = new Runnable() {
		public void run() {
			Utils.DisparitonLayoutImageTaost(context);
		}
	};
	/* Overlay Methodes */
	
	private static CalqueSites instance;

	public static CalqueSites getInstance() {

		return instance;
	}

	public static void InitialisationInsatance(Drawable marker,
			Context context, MapView mapView) {

		instance = new CalqueSites(marker, context, mapView);
	}

	private CalqueSites(Drawable marker, Context context, MapView mapView) {
		super(marker);
		this.marker = marker;
		this.context = context;
		this.mapView = mapView;

		// Renitialise() ;

	}

	public void Renitialise() {
		items = new ArrayList<OverlayItemCustom>();

		for (Mosque mosquai : Utils.getMosquais())
			if (!mosquai.getHavePicture()) {
				OverlayItemCustom item = new OverlayItemCustom(GeoTools
						.makeGeoPoint(mosquai.getLatitude(), mosquai
								.getLongitude()), mosquai.getInfo(), mosquai
						.getNom());
				item.setMosquai(mosquai);

				items.add(item);
			}
		populate();
	}

	@Override
	protected OverlayItemCustom createItem(int i) {
		return (items.get(i));
	}

	@Override
	public void draw(Canvas canvas, MapView mapView, boolean shadow) {
		if (MyApplicationMainActivity.getMyMapView().getZoomLevel() < 8) {
			populate();
		} else
			super.draw(canvas, mapView, false);

		boundCenterBottom(marker);

		if (!items.isEmpty()
				&& Utils.geoPointsAreSame(items.get(nbTaped).getPoint(),
						mapView.getMapCenter()) && animationLaunched) {

			String text = items.get(nbTaped).getTitle() + " "
					+ items.get(nbTaped).getSnippet();

			Utils.MyCustomisedViewToast(context, text, -1);

			mHandler.removeCallbacks(mUpdateTimeTask);
			mHandler.postDelayed(mUpdateTimeTask, Param.LONG_DELAY);

			animationLaunched = false;
			Utils.calqueEnum = CalqueEnum.NORMAL;

			initialiseRoute(canvas, mapView);

		}
		if (!items.isEmpty() && drawRoute && isDrawingRouteActive)
			Utils.drawPathRoute(mapView, canvas, mPoints);

	}

	// Route intitialisation

	private ArrayList<GeoPoint> mPoints;
	private Road mRoad;
	public static boolean drawRoute = false;
	Handler mHandler2 = new Handler() {
		public void handleMessage(android.os.Message msg) {

			if (mRoad.mRoute.length > 0) {
				mPoints = new ArrayList<GeoPoint>();
				for (int i = 0; i < mRoad.mRoute.length; i++) {
					mPoints.add(new GeoPoint(
							(int) (mRoad.mRoute[i][1] * 1000000),
							(int) (mRoad.mRoute[i][0] * 1000000)));
				}
			}

			drawRoute = true;
		};
	};

	private void initialiseRoute(Canvas canvas, MapView mapView) {
		Utils.setDrawRouteBooleanFalse();
		configRoute();

	}

	private void configRoute() {
		new Thread() {
			@Override
			public void run() {
				double fromLat = MyApplicationMainActivity.mLatitude, fromLon = MyApplicationMainActivity.mLongitude, toLat = items
						.get(nbTaped).getMosquai().getLatitude(), toLon = items
						.get(nbTaped).getMosquai().getLongitude();
				String url = RoadProvider
						.getUrl(fromLat, fromLon, toLat, toLon);
				InputStream is = Utils.getConnection(url);
				mRoad = RoadProvider.getRoute(is);
				mHandler2.sendEmptyMessage(0);

			}
		}.start();
	}

	// end route inti

	private boolean animationLaunched = false;

	private int nbTaped;

	@Override
	protected boolean onTap(int i) {

		mapView.getController().animateTo(items.get(i).getPoint());
		animationLaunched = true;
		nbTaped = i;

		return (true);
	}

	@Override
	public int size() {

		return (items.size());
	}

	public void Populate() {
		populate();
	}

}